/**
 * 
 */
package edu.cornell.cs.lsi.projone.service;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

import edu.cornell.cs.lsi.projone.model.SessionTableData;

/**
 * @author poornima
 * 
 */
public class BrickManager {

	private final static Log LOGGER = LogFactoryImpl.getLog(BrickManager.class);

	private final SessionTableManager sessionTableMgr;

	public BrickManager() {
		sessionTableMgr = new SessionTableManager();
	}

	public void saveServerData(String sessionId, String sessionVer,
			String sessionCreationTime, int counter, String sessionVal) {

		LOGGER.info("\n");
		LOGGER.info("----------------------------SAVE printing session table-------------------------------- BEFORE\n");
		sessionTableMgr.displaySessionMap();
		LOGGER.info("-------------- printing session table ---------------- BEFORE\n");
		LOGGER.info("\n");

		SessionTableData sessionData = new SessionTableData();
		sessionData.setSessionCreationTime(sessionCreationTime);
		sessionData.setSessionID(sessionId);
		sessionData.setVersionNumber(Double.parseDouble(sessionVer));
		sessionData.setCounter(counter);
		sessionData.setSessionVal(sessionVal);
		sessionTableMgr.addEntry(sessionData);

		LOGGER.info("\n");
		LOGGER.info("\n");
		LOGGER.info("--------------SAVE printing session table ---------------- AFTER");
		sessionTableMgr.displaySessionMap();
		LOGGER.info("-------------- printing session table ---------------- AFTER");
		LOGGER.info("\n");
		LOGGER.info("\n");

	}

	public SessionTableData getSessionData(String sessionId,
			String sessionVersion) {
		if (sessionId == null || StringUtils.isEmpty(sessionId)
				|| sessionVersion == null
				|| StringUtils.isEmpty(sessionVersion)) {
			LOGGER.error("BrickManager: getSessionData(): either the session id or session version is empty.");
			return null;
		}

		LOGGER.info("\n");
		LOGGER.info("\n");
		LOGGER.info("--------------GET printing session table ---------------- BEFORE");
		sessionTableMgr.displaySessionMap();
		LOGGER.info("-------------- printing session table ---------------- AFTER");
		LOGGER.info("\n");
		LOGGER.info("\n");

		return sessionTableMgr.getEntry(sessionId,
				Double.parseDouble(sessionVersion));
	}

	public void deleteSessionData(String sessionId) {
		if (sessionId == null || StringUtils.isEmpty(sessionId)) {
			LOGGER.error("BrickManager: deleteSessionData(): either the session id or session version is empty.");
			return;
		}

		LOGGER.info("\n");
		LOGGER.info("\n");
		LOGGER.info("--------------DEL printing session table ---------------- BEFORE");
		sessionTableMgr.displaySessionMap();
		LOGGER.info("-------------- printing session table ---------------- BEFORE");
		LOGGER.info("\n");
		LOGGER.info("\n");

		sessionTableMgr.removeEntry(sessionId);

		LOGGER.info("\n");
		LOGGER.info("\n");
		LOGGER.info("--------------SAVE printing session table ---------------- AFTER");
		sessionTableMgr.displaySessionMap();
		LOGGER.info("-------------- printing session table ---------------- AFTER");
		LOGGER.info("\n");
		LOGGER.info("\n");
	}
}